
//密码登录和短信登录切换

$(function(){
	$(".smsBtn").click(function(){		
		$(this).addClass("hide");
		$(".pwdBtn").removeClass("hide");
		$(".pwd-login").addClass("hide");
		$(".sms-login").removeClass("hide");
	});
	$(".pwdBtn").click(function(){
		$(this).addClass("hide");
		$(".smsBtn").removeClass("hide");
		$(".sms-login").addClass("hide");
		$(".pwd-login").removeClass("hide");
	});
})


var oPhone,oPassword,oCode,ophCode;
$(".content li input").blur(function(){
	$(this).addClass("input-erro")
})

$(".content li").on("focus","input",function(){
	$(this).removeClass("input-erro");
	$(this).parent().find("i").html("")
})

//密码登录验证
//手机号验证
function pic1(){
	userPhone = $("#userName").val();
	//console.log(userPhone);
	var  Num = /^1[34578]\d{9}$/;
	if(userPhone == ""){
		$(".pwd-login li:nth-child(1) i").html("请输入手机号");
		$(".pwd-login li:nth-child(1) input").addClass("input-erro");
		
	}else if( Num.test(userPhone) ){
		$("li:nth-child(1) input").removeClass("input-erro");
		
		oPhone = true;
	}else{
		$("li:nth-child(1) i").html("账号格式错误，请重新输入");
		
	}
}
$("#userName").blur(function(){
	pic1();
});

//密码验证
function pic2(){
	userPassword = $("#password").val();
	var Pas = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$/ ;
	if(userPassword == ""){
		$("li:nth-child(2) i").html("请输入登录密码");
	}else if( Pas.test(userPassword) ){
		$("li:nth-child(2) input").removeClass("input-erro");
		oPassword = true;
	}else{
		$("li:nth-child(2) i").html("密码格式不正确");
	}
}
$("#password").blur(function(){
	pic2();
});


$(".login-auto span b").click(function(){
	$(this).toggleClass("login-remember");
	
})

$(".btn").click(function(){
	pic1();
	pic2();
	if(oPhone == true&&oPassword == true){
		//alert("确认提交？")
//		var arr = [];			
			oldCookie = getCookie("userlist");
			//console.log(oldCookie);
			var flag = true;
			if(oldCookie.length != 0 ){
				arr = oldCookie;//取出cookie存入数组
				var flag = true;
				for(var i in arr){
					if(userPhone == arr[i].name && userPassword == arr[i].number){
						alert("登录成功");
						if($(".login-auto span b").ClassName=="login-remember"){
							setCookie("zxcuser",userPhone,7,"/");
						}else{
							setCookie("zxcuser",userPhone,"/");
						}
						location.href = "../index.html";
						return;
					}else if(userPhone == arr[i].name && userPassword != arr[i].number){
						alert("密码错误，请确认");
						location.reload(true);
						return;
					}else if(i == arr.length-1){
						alert("该账号未注册，请先注册");
						location.href="register.html";
						return;
					}
			}
			
		}else{
			alert("该账号未注册，请先注册");
			location.href="register.html";
			return;
		}
	}
})



//短信登录验证
//手机号验证
function pic3(){
	smPhone = $("#smsPhone").val();
	//console.log(userPhone);
	var  Num = /^1[34578]\d{9}$/;
	if(smPhone == ""){
		$(".sms-login li:nth-child(1) i").html("请输入手机号");
		$(".sms-login li:nth-child(1) input").addClass("input-erro");
		
	}else if( Num.test(smPhone) ){
		$(".sms-login li:nth-child(1) input").removeClass("input-erro");
		$(".sms-login .smsCode span").click( function(){
			
			$(".sms-login li:nth-child(1) i").html("验证码已发出，请在10分钟内完成输入");
			$(".smsCode b").html(parseInt(Math.random()*900000+100000));
		});
		oPhone = true;
	}else{
		$(".sms-login li:nth-child(1) i").html("账号格式错误，请重新输入");
		
	}
}
$("#smsPhone").blur(function(){
	pic3();
});

function pic4(){
	phCode = $("#smsCode").val();
	if(phCode == ""){
		$(".smsCode i").html("请输入短信验证码");
	}else if(phCode == $("li:nth-child(2) b").html()){
		$(".smsCode input").removeClass("input-erro");
		ophCode = true;
	}else{
		$(".smsCode i").html("请确认短信验证码");
	}
}
$("#smsCode").blur(function(){
	pic4();
})

$(".btn").click(function(){
	pic3();
	pic4();
	if(oPhone==true&&ophCode==true){
		alert("登录成功")
		location.href = "../index.html";
	}
})




